package com.feicheng.iou.controller.weChat;

import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.weChatService.WeChatLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/iou/weixin")
@Slf4j
public class ApiLoginController {

    @Autowired
    private WeChatLoginService weChatLoginService;

    /**
     * @param code 授权码
     * @param state 用户自定义参数,原样返回
     * @description  调用微信授权接口,根据获取到的code换取openid
     * @return ResultInfoV110
     */
    @PostMapping("/wxH5login")
    public ResultInfoV110 getOpenId(String code, String state){
        log.info("进入ApiLoginController的getOpenId方法,参数code为"+code+",参数state为"+state);
        //校验参数是否为空
        if(code == null){
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002,"用户微信登录的授权码code获取"));
        }
        return weChatLoginService.getOpenId(code, state);
    }

    //测试Controller
    @GetMapping("/test")
    public String testController(){
        log.info("test.....");
        return "String";
    }

}
